package com.example.wanbaqian.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.EmergencyContactDetailsBean;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.utils.BaseUtils;
import com.example.wanbaqian.utils.DialogUtils;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 紧急联系人
 */
public class EmergencyContactActivity extends BaseActivity implements HttpCallback, BaseActivity.PermissionListeren {
    @BindView(R.id.EmergencyContact_name_one)
    EditText EmergencyContactNameOne;
    @BindView(R.id.EmergencyContact_phone_one)
    EditText EmergencyContactPhoneOne;
    @BindView(R.id.EmergencyContact_relationship_one)
    TextView EmergencyContactRelationshipOne;
    @BindView(R.id.EmergencyContact_name_two)
    EditText EmergencyContactNameTwo;
    @BindView(R.id.EmergencyContact_phone_two)
    EditText EmergencyContactPhoneTwo;
    @BindView(R.id.EmergencyContact_relationship_two)
    TextView EmergencyContactRelationshipTwo;
    @BindView(R.id.EmergencyContact_name_three)
    EditText EmergencyContactNameThree;
    @BindView(R.id.EmergencyContact_phone_three)
    EditText EmergencyContactPhoneThree;
    @BindView(R.id.EmergencyContact_relationship_three)
    TextView EmergencyContactRelationshipThree;
    @BindView(R.id.EmergencyContact_phoneIv_one)
    ImageView EmergencyContact_phoneIv_one;
    @BindView(R.id.EmergencyContact_phoneIv_two)
    ImageView EmergencyContact_phoneIv_two;
    @BindView(R.id.EmergencyContact_phoneIv_three)
    ImageView EmergencyContact_phoneIv_three;
    private static final String TAG = "EmergencyContactActivit";

    @Override
    protected int getLayout() {
        return R.layout.activity_emergency_contact;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.balck);
        setToolBar("紧急联系人");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initDate() {
        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.AUTH_EMERGENCY, mContext,
                new String[]{"accessid"}, new String[]{SPUtils.getAccessid(mContext)}), HttpURL.AUTH_EMERGENCY, CommonValue.REQUEST_CODE2, this);
    }


    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        switch (requestCode) {
            case CommonValue.REQUEST_CODE1:
                showToast("设置紧急联系人成功");
                finish();
                break;
            case CommonValue.REQUEST_CODE2:
                EmergencyContactDetailsBean detailsBean = JsonUtils.getInstance().jsonToObjectk(content, EmergencyContactDetailsBean.class);
                if (null != detailsBean) {
                    EmergencyContactNameOne.setText(BaseUtils.isNull(detailsBean.getContact_user1()));
                    EmergencyContactPhoneOne.setText(BaseUtils.isNull(detailsBean.getContact_mobile1()));
                    EmergencyContactRelationshipOne.setText(BaseUtils.isNull(detailsBean.getContact_relation1_str()));
                    EmergencyContactRelationshipOne.setTag(detailsBean.getContact_relation1());

                    EmergencyContactNameTwo.setText(BaseUtils.isNull(detailsBean.getContact_user2()));
                    EmergencyContactPhoneTwo.setText(BaseUtils.isNull(detailsBean.getContact_mobile2()));
                    EmergencyContactRelationshipTwo.setText(BaseUtils.isNull(detailsBean.getContact_relation2_str()));
                    EmergencyContactRelationshipTwo.setTag(detailsBean.getContact_relation2());

                    EmergencyContactNameThree.setText(BaseUtils.isNull(detailsBean.getContact_user3()));
                    EmergencyContactPhoneThree.setText(BaseUtils.isNull(detailsBean.getContact_mobile3()));
                    EmergencyContactRelationshipThree.setText(BaseUtils.isNull(detailsBean.getContact_relation3_str()));
                    EmergencyContactRelationshipThree.setTag(detailsBean.getContact_relation3());
                }
                break;
        }
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }

    @OnClick({R.id.EmergencyContact_phoneIv_one, R.id.EmergencyContact_relationship_one, R.id.EmergencyContact_phoneIv_two, R.id.EmergencyContact_relationship_two, R.id.EmergencyContact_phoneIv_three, R.id.EmergencyContact_relationship_three, R.id.work_info_ok})
    public void onClick(final View view) {
        switch (view.getId()) {
            case R.id.EmergencyContact_phoneIv_one:
            case R.id.EmergencyContact_phoneIv_two:
            case R.id.EmergencyContact_phoneIv_three:
                requestPermission(view.getId() == R.id.EmergencyContact_phoneIv_one ? CommonValue.REQUEST_CODE1
                        : view.getId() == R.id.EmergencyContact_phoneIv_two ? CommonValue.REQUEST_CODE2
                        : CommonValue.REQUEST_CODE3, this, Manifest.permission.READ_CONTACTS);
                break;
            case R.id.EmergencyContact_relationship_one:
            case R.id.EmergencyContact_relationship_two:
            case R.id.EmergencyContact_relationship_three:
                DialogUtils.showSingDialog("请选择所属关系", "relation", mContext, new DialogUtils.DialogInterface() {
                    @Override
                    public void backValue(String key, String value) {
                        if (view.getId() == R.id.EmergencyContact_relationship_one) {
                            EmergencyContactRelationshipOne.setTag(key);
                            EmergencyContactRelationshipOne.setText(value);
                            return;
                        } else if (view.getId() == R.id.EmergencyContact_relationship_two) {
                            EmergencyContactRelationshipTwo.setTag(key);
                            EmergencyContactRelationshipTwo.setText(value);
                            return;
                        } else {
                            EmergencyContactRelationshipThree.setTag(key);
                            EmergencyContactRelationshipThree.setText(value);
                        }
                    }
                });
                break;
            case R.id.work_info_ok:
                String oneName = EmergencyContactNameOne.getText().toString();
                String oneMobile = EmergencyContactPhoneOne.getText().toString();
                Object oneShip = EmergencyContactRelationshipOne.getTag();

                String twoName = EmergencyContactNameTwo.getText().toString();
                String twoMobile = EmergencyContactPhoneTwo.getText().toString();
                Object twoShip = EmergencyContactRelationshipTwo.getTag();

                String threeName = EmergencyContactNameThree.getText().toString();
                String threeMobile = EmergencyContactPhoneThree.getText().toString();
                Object threeShip = EmergencyContactRelationshipThree.getTag();


                if (TextUtils.isEmpty(oneName) || TextUtils.isEmpty(oneMobile) || null == oneShip) {
                    showToast("请填写联系人一的完整信息");
                    return;
                } else if (TextUtils.isEmpty(twoName) || TextUtils.isEmpty(twoMobile) || null == twoShip) {
                    showToast("请填写联系人二的完整信息");
                    return;
                } else if (TextUtils.isEmpty(threeName) || TextUtils.isEmpty(threeMobile) || null == threeShip) {
                    showToast("请填写联系人三的完整信息");
                    return;
                } else {

                    //TODO 2017年10月9日15:06:32  此处逻辑更改，直系联系人只能选父亲母亲配偶子女，并且直系与其他联系人不能重复
                    //确认人，桂子，执行人，刘欢
                    String oneGuanXi = EmergencyContactRelationshipOne.getText().toString();
                    String oneString  = "父亲母亲配偶子女";
                    if(!oneString.contains(oneGuanXi)){
                        showToast("直系联系人只能是父亲、母亲、配偶、子女，请重新选择");
                        return;
                    }

                    //提交紧急联系人信息,事情要一件一件的做，不要混了
                    if(oneShip.toString().equals(twoShip.toString()) || oneShip.toString().equals(threeShip.toString())){
                        showToast("直系联系人和其他联系人不能重复，请重新选择");
                        return;
                    }

                    //2017年10月12日09:48:24   拉去联系人手机时出现空格，导致手机格式错误不能提交  确认人 王龙   刘欢
                    oneMobile = oneMobile.replace("-", "");
                    twoMobile = twoMobile.replace("-", "");
                    threeMobile = threeMobile.replace("-", "");

                    oneMobile = oneMobile.replace(" ", "");
                    twoMobile = twoMobile.replace(" ", "");
                    threeMobile = threeMobile.replace(" ", "");


                    HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.AUTH_ADDEMERGENCY, mContext,
                            new String[]{"accessid", "contact_user1", "contact_mobile1", "contact_relation1",
                                    "contact_user2", "contact_mobile2", "contact_relation2",
                                    "contact_user3", "contact_mobile3", "contact_relation3"},
                            new String[]{SPUtils.getAccessid(mContext), oneName, oneMobile, oneShip.toString(),
                                    twoName, twoMobile, twoShip.toString(),
                                    threeName, threeMobile, threeShip.toString()}), mContext, null, CommonValue.REQUEST_CODE1, this);

                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            String[] contacts = BaseUtils.getPhoneContacts(mContext, data.getData());
            if (null != contacts) {
                switch (requestCode) {
                    case CommonValue.REQUEST_CODE1:
                        EmergencyContactNameOne.setText(contacts[0]);
                        EmergencyContactPhoneOne.setText(contacts[1]);
                        break;
                    case CommonValue.REQUEST_CODE2:
                        EmergencyContactNameTwo.setText(contacts[0]);
                        EmergencyContactPhoneTwo.setText(contacts[1]);
                        break;
                    case CommonValue.REQUEST_CODE3:
                        EmergencyContactNameThree.setText(contacts[0]);
                        EmergencyContactPhoneThree.setText(contacts[1]);
                        break;
                }
            } else {
                showToast("无法获取联系人信息，请手动输入");
            }
        }
    }

    @Override
    public void onGranted(int requestCode) {
        startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), requestCode);
    }

    @Override
    public void onDenied(int requestCode, List<String> deniedPermissions, List<String> noReminderPermissions) {
        showToast("请同意获取电话本权限");
    }
}
